package class2.api;

import class2.pojo.User;
import class2.pojo.Cfg;
import class2.service.UserService;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Objects;

public class Api {
    private final UserService service;

    public Api() {
        service = new UserService();
    }

    public void doProcess(String paramString) throws InvocationTargetException, IllegalAccessException {
        String[] param = paramString.split(" ");
        if ("help".equals(param[0])) {
            System.out.println(Cfg.HELP);
        } else if ("add".equals(param[0])) {
            apiAdd(param);
        } else if ("mod".equals(param[0])) {
            apiMod(param);
        } else if ("delete".equals(param[0])) {
            apiDelete(param);
        } else if ("show".equals(param[0])) {
            apiShow(param);
        } else if ("list".equals(param[0])) {
            apiList(param);
        } else if(!paramString.isEmpty()){
            noCmd(param[0]);
        }
    }

    private void noCmd(String param) {
        System.out.println(Cfg.NO_SUCH_CMD + param);
    }

    private void apiList(String[] param) {
        HashMap<String, String> map = new HashMap<String, String>(){{put("all","");}};
        for (int i = 1; i < param.length; i++) {
            String[] paramSplit = param[i].split(":");
            if (paramSplit.length != 2) {
                System.out.println(Cfg.LIST_LEGAL);
                return;
            }
            map.put(paramSplit[0], paramSplit[1]);
            if(!Objects.equals(paramSplit[0], "order")){
                map.remove("all");
            }
        }
        service.showAll(map).forEach(System.out::println);
    }

    private void apiShow(String[] param) {
        if (param.length != 2) {
            System.out.println(Cfg.SHOW_LEGAL);
        }
        String userInfo = param[1];
        String[] userKv = userInfo.split(":");
        if (!(userKv.length == 2)) {
            System.out.println("key value 异常");
        }
        User out = this.service.show(userKv[0],userKv[1]);
        System.out.println(out);
    }

    private void apiDelete(String[] param) {
        if (param.length != 2) {
            System.out.println(Cfg.DELETE_LEGAL);
        } else {
            service.delete(param[1]);
        }
    }

    private void apiMod(String[] param) throws InvocationTargetException, IllegalAccessException {
        if (param.length != 3) {
            System.out.println(Cfg.MOD_LEGAL);
        }
        String[] anyList = param[2].split(":");
        if (anyList.length != 2) {
            System.out.println(Cfg.MOD_KEY_LEGAL);
        } else {
            service.mod(param[1], anyList[0], anyList[1]);
        }
    }

    SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");
    private void apiAdd(String[] param) {
        if (param.length > 6 || param.length < 1) {
            System.out.println(Cfg.ADD_LEGAL);
        }
        HashMap<String, String> userMap = new HashMap<>();
        for (int i = 1; i < param.length; i++) {
            String userInfo = param[i];
            String[] userKv = userInfo.split(":");
            if (!(userKv.length == 2)) {
                System.out.println("key value ");
            }
            if("date".equals(userKv[0])){
                try {
                    format.parse(userKv[1]);
                } catch (ParseException e) {
                    System.out.println("日期格式异常");
                    return;
                }
            }
            userMap.put(userKv[0], userKv[1]);
        }
        int ret = service.add(userMap);
        if(ret==-1){
            System.out.println("id已存在");
        }else if(ret==-2){
            System.out.println("id必须为纯数字");
        }
    }
}
